#include <sqlite3.h>
#include <sstream>
#include "MapTile.h"
#include "Logger.h"
#include "Game.h"

MapTile::MapTile(int tileID) {
	// Initialize the Character with data from the Character table
	sqlite3* db;
	sqlite3_stmt* sqlQuery;
	int errcode;

	// connect to game database
	errcode = sqlite3_open("gamedb.s3db", &db);

	if(errcode) {
		Logger::Log(E_DB_CONNECT_FAILED, "SQL ERROR: Failed to open database", __FILE__, __LINE__, LOG_ERROR);
		MessageBox(NULL, L"Failed to open database", L"ERROR", MB_OK | MB_ICONERROR);
		sqlite3_close_v2(db);
	}
	
	ostringstream statement;

	statement << "SELECT * FROM map_tiles WHERE id = " << tileID << ";";

	if(sqlite3_prepare_v2(db, statement.str().c_str(), -1, &sqlQuery, NULL) != SQLITE_OK) Logger::Log(E_DB_QUERY_FAILED, "SQL ERROR: Failed to prepare query", __FILE__, __LINE__, LOG_ERROR);

	int tileTypeID = 0;

	if(sqlQuery != NULL) {
		while(sqlite3_step(sqlQuery) == SQLITE_ROW) {
			tileTypeID = sqlite3_column_int(sqlQuery, 1);
			this->xPos = sqlite3_column_double(sqlQuery, 2);
			this->yPos = sqlite3_column_double(sqlQuery, 3);
		}
	}

	sqlite3_finalize(sqlQuery);

	statement.str("");
	statement.clear();

	statement << "SELECT * FROM tile_type WHERE id = " << tileTypeID << ";";

	if(sqlite3_prepare_v2(db, statement.str().c_str(), -1, &sqlQuery, NULL) != SQLITE_OK) Logger::Log(E_DB_QUERY_FAILED, "SQL ERROR: Failed to prepare query", __FILE__, __LINE__, LOG_ERROR);

	int width = 0;
	int height = 0;

	if(sqlQuery != NULL) {
		while(sqlite3_step(sqlQuery) == SQLITE_ROW) {
			this->textureFile = (char*)sqlite3_column_text(sqlQuery, 1);
			width = sqlite3_column_int(sqlQuery, 2);
			height = sqlite3_column_int(sqlQuery, 3);
		}
	}

	sqlite3_finalize(sqlQuery);

	sqlite3_close_v2(db);

	this->textureID = Game::GraphicsPtr->LoadMapTileTexture(this->textureFile);

	this->FillQuad(width, height);
}

void MapTile::FillQuad(int width, int height) {

	this->Quad[0].Color = D3DXCOLOR(0.5f, 0.5f, 0.0f, 1.0f);
	this->Quad[1].Color = D3DXCOLOR(0.5f, 0.5f, 0.0f, 1.0f);
	this->Quad[2].Color = D3DXCOLOR(0.5f, 0.5f, 0.0f, 1.0f);
	this->Quad[3].Color = D3DXCOLOR(0.5f, 0.5f, 0.0f, 1.0f);

	this->Quad[0].U = 0.0f;
	this->Quad[0].V = 0.0f;
		
	this->Quad[1].U = 1.0f;
	this->Quad[1].V = 0.0f;
		
	this->Quad[2].U = 0.0f;
	this->Quad[2].V = 1.0f;
		
	this->Quad[3].U = 1.0f;
	this->Quad[3].V = 1.0f;

	this->Quad[0].Z = 0.0f;
	this->Quad[1].Z = 0.0f;
	this->Quad[2].Z = 0.0f;
	this->Quad[3].Z = 0.0f;

	float truewidth = (float)width / 64.0f;
	float trueheight = (float)height / 64.0f;

	this->Quad[0].X = 0.0f - (truewidth / 2.0f);
	this->Quad[0].Y = 0.0f + (trueheight / 2.0f);

	this->Quad[1].X = 0.0f + (truewidth / 2.0f);
	this->Quad[1].Y = 0.0f + (trueheight / 2.0f);

	this->Quad[2].X = 0.0f - (truewidth / 2.0f);
	this->Quad[2].Y = 0.0f - (trueheight / 2.0f);

	this->Quad[3].X = 0.0f + (truewidth / 2.0f);
	this->Quad[3].Y = 0.0f - (trueheight / 2.0f);
}